package com.jlz.managers.user;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jlz.beans.def.SiteBean;
import com.jlz.beans.user.LinkBean;
import com.jlz.beans.user.UserBean;
import com.jlz.managers.def.Sites;
import com.julewa.db.DB;
import com.julewa.utils.Patterns;

@Service 
public class Links {
	public enum State{
		NEW(0),
		USING(1),
		LOCKED(3)
		;
		private int value = 0;
		private State(int v){value = v;}
		public int getValue(){return value;};
		public String getName(){return name();};
		public static State of(int v){
			for(State s:State.values()){
				if(s.value == v)return s;
			}
			return null;
		}
	}
	
	private @Autowired DB db;
	private @Autowired Sites sites;
	public LinkBean create(){
		return db.create(LinkBean.class);
	}
	
	public LinkBean find(String usercode){
		LinkBean link = create();
		if(usercode == null)return link;
		usercode = usercode.trim().toLowerCase();
		int idx = usercode.indexOf("@");
		if(idx < 0){
			link.setCode(usercode);
			if(Patterns.MOBILE.match(usercode)){
				link.setSiteId(Sites.MOBILE);
			}else{
				link.setSiteId(Sites.JULEZU);
			}
		}else{
			link.setCode(usercode.substring(0, idx));
			SiteBean site = sites.find(usercode.substring(idx + 1));
			if(site != null){
				link.setSiteId(site.getID());
			}else{
				link.setSiteId(Sites.PHONE);
			}
		}
		db.getObject("link.bySiteAndCode", link, link);
		return link;
	}
	
	@SuppressWarnings("unchecked")
	public void load(UserBean userbean){
		if(null != userbean.getLinks())return;
		userbean.setLinks(db.getList("link.ofUser", userbean));
	}
	
	public void append(LinkBean link){
		db.insert("link.insert", link);
	}
	
	public void update(LinkBean link){
		db.update("link.update", link);
	}
	
	public void remove(LinkBean link){
		db.delete("link.delete", link);
	}
	
	private double random = 0.31254986; 
	public String randomPassword(){
		random += Math.random();
		String str = Double.toString(random);
		str.replace('.', '7');
		if(str.length() < 6) str += str;
		return str.substring(str.length() - 6);
	}
	
	//一旦运行,就不可再改了
	private static final int cypher_random1 = 321329;
	private static final int cypher_random2 = 913507;
	public String encode(LinkBean b){
		StringBuffer buf = new StringBuffer();
		buf.append("-");
		buf.append(b.getUserId() + cypher_random1);
		buf.append("-");
		buf.append(b.getSiteId() + cypher_random2);
		buf.append("-");
		buf.append(b.getCode());
		return buf.toString();
	}
	
	public LinkBean decode(String cypher){
		int idx = cypher.indexOf(1, '-');
		long userid = Long.parseLong(cypher.substring(1, idx)) - cypher_random1;
		cypher = cypher.substring(idx + 1);
		idx = cypher.indexOf('-');
		long siteid = Long.parseLong(cypher.substring(0, idx)) - cypher_random2;
		cypher = cypher.substring(idx + 1);
		//
		LinkBean link = new LinkBean();
		link.setCode(cypher);
		link.setSiteId((int)siteid);
		//
		db.getObject("link.object", link, link);
		if(link.getUserId() == null || !link.getUserId().equals(userid))return null;
		return link;
	}

	@SuppressWarnings("unchecked")
	public List<LinkBean> getLinksOf(UserBean userBean) {
		return db.getList("link.ofUser", userBean);
	}
	
}
